//
//  ViewController.m
//  ViewMoveAccordingToKeyboard
//
//  Created by 裴波波 on 16/6/1.
//  Copyright © 2016年 裴波波. All rights reserved.
//

#import "ViewController.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScreenB [UIScreen mainScreen].bounds

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *txtInput;



@end

@implementation ViewController





- (void)viewDidLoad {
    [super viewDidLoad];
    
    /** 添加通知 */
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(moveKeyboard:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

-(void)moveKeyboard:(NSNotification *)notification{

    /** 键盘完全弹出时间 */
    NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] intValue];

    /** 动画趋势 */
    int curve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];
    
    /** 动画执行完毕frame */
    CGRect keyboard_frame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    /** 获取键盘y值 */
    CGFloat keyboard_y = keyboard_frame.origin.y;
    
    /** view上平移的值 */
    CGFloat offset = kScreenH - keyboard_y;

    /** 执行动画  */
    [UIView animateWithDuration:duration animations:^{
       
        [UIView setAnimationCurve:curve];
        self.view.transform = CGAffineTransformMakeTranslation(0, -offset);
    }];
    
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
